package cn.zhaoguang.usedcar.main.controller;

import cn.zhaoguang.usedcar.common.http.ResultVO;
import cn.zhaoguang.usedcar.main.entity.Collection;
import cn.zhaoguang.usedcar.main.service.CollectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Description
 * @Author zhaoguang
 * @Date 2021/7/5 21:53
 */
@RestController
@RequestMapping("/collection")
@Api(tags = "用户收藏控制器")
public class CollectionController {

    @Autowired
    private CollectionService collectionService;

    @PostMapping("/add")
    @ApiOperation(value = "添加", notes = "添加收藏(返回id是为了用户又继续取消了收藏)")
    public ResultVO add(@RequestBody Collection collection) {
        int result = collectionService.add(collection);
        if (result > 0) {
            return ResultVO.success("收藏成功", collection.getId());
        }
        return ResultVO.error("收藏失败", result);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除", notes = "根据id删除收藏")
    public ResultVO delete(@PathVariable("id") Integer id) {
        int result = collectionService.delete(id);
        if (result > 0) {
            return ResultVO.success("取消收藏");
        }
        return ResultVO.error("取消收藏失败");
    }

    @PostMapping("/findAll")
    @ApiOperation(value = "查询我的收藏", notes = "查询我的收藏")
    public ResultVO findAll(@RequestBody Map<String, Integer> map) {
        Integer userId = map.get("id");
        return ResultVO.success("查询成功", collectionService.findAll(userId));
    }
}
